package com.example.fragmentdemo;


import java.util.HashMap;
import java.util.List;

import cn.wpw.addressbook.R;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("NewApi")
public class Texture extends Fragment implements AdapterView.OnItemClickListener {
	private PhoneDB mPhoneDB;
	private Cursor mPhone;
	private ListView PhoneList;
	private View view;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.texture_layout,
				container, false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		PhoneList = (ListView) view.findViewById(R.id.IDtexture);
		mPhoneDB = new PhoneDB(getActivity());
		mPhone = mPhoneDB.select();
		mPhone.requery();
		PhoneList.invalidateViews();

		PhoneList.setAdapter(new PhoneListAdapter(this, mPhone));

		PhoneList.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		int i = 0;
		for (i = 0; i <1000; i++) {
			switch (arg2) {
			
			case 0:
				System.out.println(arg1.getId());
				break;
			case 2:
				System.out.println(arg1.getId());
				break;
			case 3:
				System.out.println(arg1.getId());
				break;
			case 4:
				System.out.println(arg2);
				break;
			case 5:
				System.out.println(arg1.getId());
				break;
			default:
				break;
			}
		}
		
	}
	public class PhoneListAdapter extends BaseAdapter {
		private Context mContext;
		private Cursor mCursor;

		public PhoneListAdapter(Texture texture, Cursor mPhone) {
			mContext = texture.getActivity();
			mCursor = mPhone;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mCursor.getCount();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView mTextView = new TextView(mContext);
			mCursor.moveToPosition(position);
			mTextView.setText(mCursor.getString(1));
			
			return mTextView;
		}

	}
}
